package com.ruoyi.errorLib.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.paper.domain.ExamOption;
import com.ruoyi.paper.domain.ExamPaperLib;
import com.ruoyi.paper.service.IExamOptionService;
import com.ruoyi.paper.service.IExamPaperLibService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.errorLib.mapper.ErrorQuestionMapper;
import com.ruoyi.errorLib.domain.ErrorQuestion;
import com.ruoyi.errorLib.service.IErrorQuestionService;
import com.ruoyi.common.utils.uuid.IdUtils;

/**
 * 错题本Service业务层处理
 */
@Service
public class ErrorQuestionServiceImpl implements IErrorQuestionService {
    @Autowired
    private ErrorQuestionMapper errorQuestionMapper;
    @Autowired
    private IExamOptionService examOptionService;
    @Autowired
    private IExamPaperLibService paperLibService;
    /**
     * 查询错题
     */
    @Override
    public ErrorQuestion selectErrorQuestionById(String errorId) {
           ErrorQuestion question=errorQuestionMapper.selectErrorQuestionById(errorId);
            ExamOption option=new ExamOption();
            option.setLibId(question.getQuestionId());
            List<ExamOption> options=examOptionService.selectExamOptionList(option);
            question.setOptions(options);
        return question;
    }

    /**
     * 查询错题列表
     */
    @Override
    public List<ErrorQuestion> selectErrorQuestionList(ErrorQuestion errorQuestion) {
        return errorQuestionMapper.selectErrorQuestionList(errorQuestion);
    }

    /**
     * 新增错题
     */
    @Override
    public int insertErrorQuestion(ErrorQuestion errorQuestion) {
        return errorQuestionMapper.insertErrorQuestion(errorQuestion);
    }

    /**
     * 修改错题
     */
    @Override
    public int updateErrorQuestion(ErrorQuestion errorQuestion) {
        return errorQuestionMapper.updateErrorQuestion(errorQuestion);
    }

    /**
     * 批量删除错题
     */
    @Override
    public int deleteErrorQuestionByIds(String[] errorIds) {
        return errorQuestionMapper.deleteErrorQuestionByIds(errorIds);
    }

    /**
     * 删除错题信息
     */
    @Override
    public int deleteErrorQuestionById(String errorId) {
        return errorQuestionMapper.deleteErrorQuestionById(errorId);
    }

    /**
     * 根据学生ID查询错题列表
     */
    @Override
    public List<ErrorQuestion> selectErrorQuestionByStudentId(String studentId) {
        return errorQuestionMapper.selectErrorQuestionByStudentId(studentId);
    }

    /**
     * 保存学生错题
     */
    @Override
    public int saveStudentErrorQuestion(String studentId, Long userId, String questionId, String questionContent, 
                                      String questionType, String myAnswer, String correctAnswer) {
        ErrorQuestion errorQuestion = new ErrorQuestion();
        errorQuestion.setErrorId(IdUtils.fastSimpleUUID());
        errorQuestion.setStudentId(studentId);
        errorQuestion.setUserId(userId);
        errorQuestion.setQuestionId(questionId);
        errorQuestion.setQuestionContent(questionContent);
        errorQuestion.setQuestionType(questionType);
        errorQuestion.setMyAnswer(myAnswer);
        errorQuestion.setCorrectAnswer(correctAnswer);
        errorQuestion.setPracticeCount(1);
        errorQuestion.setErrorCount(1);
        
        // 检查是否已存在该错题
        ErrorQuestion existQuestion = errorQuestionMapper.selectErrorQuestionByStudentIdAndQuestionId(studentId, questionId);
        if (existQuestion != null) {
            // 更新练习次数和错误次数
            existQuestion.setPracticeCount(existQuestion.getPracticeCount() + 1);
            existQuestion.setErrorCount(existQuestion.getErrorCount() + 1);
            existQuestion.setMyAnswer(errorQuestion.getMyAnswer()+";"+myAnswer);
            return errorQuestionMapper.updateErrorQuestion(existQuestion);
        } else {
            // 新增错题记录
            return errorQuestionMapper.insertErrorQuestion(errorQuestion);
        }
    }
} 